package com.go.smsautosender.listview;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;

import com.go.smsautosender.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FileListViewAdapter extends BaseAdapter {
	
	Context context;
	int layoutId;
	ArrayList<FileListViewData> listDataArr;
	LayoutInflater Inflater;
	String mPosition;
	
	public FileListViewAdapter(Context _context, int _layoutId, ArrayList<FileListViewData> _listDataArr){
		 
	   context = _context;
	   layoutId = _layoutId;
	   listDataArr = _listDataArr;
	   Inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listDataArr.size();
	}

	@Override
	public String getItem(int position) {
		// TODO Auto-generated method stub
		return listDataArr.get(position).FileName;
	}
	
	public String getItemPath(int position) {
		// TODO Auto-generated method stub
		return listDataArr.get(position).FilePath;
	}
	
	public String getItemType(int position) {
		return listDataArr.get(position).ImageType;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	
	

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		 mPosition = String.valueOf(position);
		 
		 if(convertView == null){
		  convertView = Inflater.inflate(layoutId,parent,false);  
		 }
		 
		 String strFileSize = "";
		 
		 ImageView imgType = (ImageView) convertView.findViewById(R.id.icon);
		 TextView tvfileName = (TextView) convertView.findViewById(R.id.title);
		 TextView tvfileSize = (TextView) convertView.findViewById(R.id.size);

		 if (listDataArr.get(position).ImageType.equals("Directory")) {
			 imgType.setImageResource(R.drawable.folder);
//			 tvfileSize.setVisibility(View.GONE);
//			 tvfileName.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	 		strFileSize = "";
		 } else {
			 imgType.setImageResource(R.drawable.ic_launcher);
//			 tvfileSize.setVisibility(View.VISIBLE);
			 strFileSize = getStrFileSize(listDataArr.get(position).FileSize);
		 }

		 tvfileName.setText(listDataArr.get(position).FileName);
		 tvfileSize.setText(strFileSize);

		 return convertView;
	}
	
	public String getStrFileSize(long size) {
		String strSize = "";
		 
		 if(1023 > size) {
			strSize = Long.toString(size);
			strSize = strSize + " byte";
		 } else if (1023 < size && size < 1048576) {
			 strSize = longDouble2String(0, size / 1024);
			 strSize = strSize + " KB";
		 } else if (1048575 < size && size < 1073741824) {
			 strSize = longDouble2String(2, (double)size / (double)1048576);
			 strSize = strSize + " MB";
		 }
		
		return strSize;
		
	}
	
	public static String longDouble2String(int size, double value) {
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(size);
        nf.setGroupingUsed(false);
        return nf.format(value);
        
//        DecimalFormat format = new DecimalFormat(".##");
//         //�Ҽ��� �ι�° �ڸ����� �ڸ���
////        String result = 
//        return format.format(value); 
    }

}
